package com.sikao.app.utils;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.sikao.app.R;

/**
 * Created by y50 .
 */
public class TimeCountUtil extends CountDownTimer {

    private TextView countView;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public TimeCountUtil(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    public TimeCountUtil(long millisInFuture, long countDownInterval, TextView countView) {
        super(millisInFuture, countDownInterval);
        this.countView = countView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        countView.setClickable(false);//设置不能点击
        countView.setText(millisUntilFinished / 1000 + "秒");//设置倒计时时间

        //设置按钮为灰色，这时是不能点击的
        countView.setBackgroundResource(R.drawable.shap_deep_gray_two);//还原背景色
        Spannable span = new SpannableString(countView.getText().toString());//获取按钮的文字
        span.setSpan(new ForegroundColorSpan(Color.WHITE), 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//讲倒计时时间显示为红色
        countView.setText(span);
    }

    @Override
    public void onFinish() {
        countView.setText("重新获取");
        countView.setClickable(true);//重新获得点击
        countView.setBackgroundResource(R.drawable.selector_btn_red_five);//还原背景色
    }


}
